/* Copyright(C) 2025. Huawei Technologies Co.,Ltd. All rights reserved.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// Package config for the fault config main test
package config

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"testing"

	"github.com/agiledragon/gomonkey/v2"
	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"

	"ascend-common/common-utils/hwlog"
	"nodeD/pkg/common"
	"nodeD/pkg/kubeclient"
)

var (
	testErr         = errors.New("test error")
	testK8sClient   *kubeclient.ClientK8s
	testFaultConfig = &common.FaultConfig{
		FaultTypeCode: &common.FaultTypeCode{
			NotHandleFaultCodes:   []string{"00000001"},
			PreSeparateFaultCodes: []string{"00000002"},
			SeparateFaultCodes:    []string{"00000003"},
		},
	}
	testErrFaultConfig = &common.FaultConfig{}
)

func TestMain(m *testing.M) {
	const testNodeName = "test-node-name"
	var patches = gomonkey.ApplyFuncReturn(kubeclient.NewClientK8s,
		&kubeclient.ClientK8s{
			ClientSet:    fake.NewSimpleClientset(),
			NodeName:     testNodeName,
			NodeInfoName: common.NodeInfoCMNamePrefix + testNodeName,
		}, nil)
	defer patches.Reset()

	if err := setup(); err != nil {
		return
	}
	code := m.Run()
	fmt.Printf("exit_code = %v\n", code)
}

func setup() error {
	if err := initLog(); err != nil {
		return err
	}
	if err := initK8sClient(); err != nil {
		return err
	}
	return nil
}

func initLog() error {
	logConfig := &hwlog.LogConfig{
		OnlyToStdout: true,
	}
	if err := hwlog.InitRunLogger(logConfig, context.Background()); err != nil {
		fmt.Printf("init hwlog failed, %v\n", err)
		return errors.New("init hwlog failed")
	}
	return nil
}

func initK8sClient() error {
	var err error
	testK8sClient, err = kubeclient.NewClientK8s()
	if err != nil {
		hwlog.RunLog.Errorf("init k8s client failed when start, err: %v", err)
		return err
	}
	return nil
}

func fakeNodeFaultConfigCM() *v1.ConfigMap {
	data, err := json.Marshal(testFaultConfig)
	if err != nil {
		panic("marshal testFaultConfig failed")
	}
	str := string(data)
	return &v1.ConfigMap{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-mindx-dl-node-fault-config",
			Namespace: "test-mindx-dl",
		},
		Data: map[string]string{common.FaultConfigKey: str},
	}
}

func fakeWrongNodeFaultConfigCMData() []byte {
	wrongFaultConfig := &common.FaultConfig{FaultTypeCode: &common.FaultTypeCode{}}
	common.DeepCopyFaultConfig(testFaultConfig, wrongFaultConfig)
	wrongFaultConfig.FaultTypeCode.NotHandleFaultCodes = []string{"WrongFaultCodes"}
	wrongData, err := json.Marshal(wrongFaultConfig)
	if err != nil {
		panic("marshal wrong fault config error")
	}
	return wrongData
}

func fakeWrongNodeFaultConfigCM() *v1.ConfigMap {
	wrongFaultConfigCM := fakeNodeFaultConfigCM()
	wrongStr := string(fakeWrongNodeFaultConfigCMData())
	wrongFaultConfigCM.Data = map[string]string{common.FaultConfigKey: wrongStr}
	return wrongFaultConfigCM
}
